package com.example.cadmin.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.MotorLog;
import com.example.cadmin.mapper.MotorLogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 电机运行日志 服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2023-02-22
 */
@Service
public class MotorLogService extends ServiceImpl<MotorLogMapper, MotorLog> {
    @Resource
    public MotorLogMapper motorLogMapper;

    /**
     * 电机运行表格数据
     * @param page
     * @param limit
     * @param type
     * @param userName
     * @param request
     * @return
     */
    public Map<String, Object> tbaleList(int page, int limit, String type, String userName, HttpServletRequest request) {
        String number = Convert.toStr(request.getSession().getAttribute("number"));
        //参数一是当前页，参数二是每页个数
        IPage<Map<String, Object>> motorLogPage = new Page<>(page, limit);
        QueryWrapper<MotorLog> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(type)) {
            wrapper.eq("type", type);
        }
        if (StrUtil.isNotEmpty(userName)) {
            wrapper.like("user_name", userName);
        }
        wrapper.orderByDesc("create_time");
        wrapper.eq("number", number);
        motorLogPage = motorLogMapper.selectMapsPage(motorLogPage, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", motorLogPage.getTotal());
        map.put("data", motorLogPage.getRecords());
        return map;
    }
}
